package com.danco.training.bookshop.service.file;

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import com.danco.training.TextFileWorker;
import com.danco.training.bookshop.entity.Book;
import com.danco.training.bookshop.entity.Claim;
import com.danco.training.bookshop.entity.storage.Bookshop;
import com.danco.training.bookshop.entity.storage.ClaimStorage;
import com.danco.training.bookshop.service.storage.ClaimStorageService;
//import com.danco.training.controller.ClaimController;
//import com.danco.training.view.Separator;

import org.apache.log4j.Logger;

public class ClaimStorageFile {
	
	private ClaimStorage claimStorage = ClaimStorage.getClaimStorageInstance();
	private ClaimStorageService claimStorageService = Bookshop.getBookshopInstance().getClaimStorageService();
	private TextFileWorker textFileWorker;
//	private ClaimController claimController = new ClaimController();
	private static final Logger logger = Logger.getLogger(ClaimStorageFile.class);
	
	public ClaimStorageFile(ClaimStorage claimStorage, String FILE)
			throws IOException {
		try {
		Path filePath = Paths.get(FILE);
		Files.createFile(filePath);
		this.claimStorage = claimStorage;
		textFileWorker = new TextFileWorker(FILE);
}
		
		catch (FileAlreadyExistsException e){
			logger.error(e.getMessage(), e);
			Files.deleteIfExists( Paths.get(FILE));			
			Path filePath = Paths.get(FILE);
			Files.createFile(filePath);
			this.claimStorage = claimStorage;
			textFileWorker = new TextFileWorker(FILE);
			
		}
		catch (Exception e){
			logger.error(e.getMessage(), e);
		}
	}

	public void saveClaimsToFile() {
try {
		String[] claims = new String[claimStorage.getClaims().size()];
		for (int i = 0; i < claims.length; i++) {
		//	claims[i] = claimController.claimDetails(claimStorage.getClaims().get(i).getId());
		}
		textFileWorker.writeToFile(claims);
}
catch (Exception e){
	logger.error(e.getMessage(), e);
}
	}

	public List<Claim> readClaimsFromFile() {

		List<Claim> claims = new ArrayList<Claim>();
		try{
		String[] claimsArray = textFileWorker.readFromFile();
		
		for (int i = 0; i < claimsArray.length; i++) {
//			int index1 = claimsArray[i].indexOf(Separator.CLAIM_SEPARATOR.toString());
//			int index2 = claimsArray[i].indexOf(Separator.CLAIM_SEPARATOR.toString(), index1+1);
//			int index3 = claimsArray[i].indexOf(Separator.BOOK_SEPARATOR.toString());
//			int index4 = claimsArray[i].indexOf(Separator.BOOK_SEPARATOR.toString(), index3+1);
//			int index5 = claimsArray[i].indexOf(Separator.BOOK_SEPARATOR.toString(), index4+1);
//			Book b = new Book(Integer.valueOf(claimsArray[i].substring(index1+1, index3)),
//					claimsArray[i].substring(index3+1, index4),claimsArray[i].substring(index4+1, index5),claimsArray[i].substring(index5+1,index2));
//			Claim claim = new Claim(Integer.valueOf(claimsArray[i].substring(0, index1)),b,Integer.valueOf(claimsArray[i].substring(index2+1)));
//			claims.add(claim);
		}
		}
		catch (StringIndexOutOfBoundsException e){
			logger.error(e.getMessage(), e);
		}
        catch (Exception e){
        	logger.error(e.getMessage(), e);
        	
		}
					

		return claims;

	}

	public ClaimStorageService getClaimStorageService() {
		return claimStorageService;
	}

	public void setClaimStorageService(ClaimStorageService claimStorageService) {
		this.claimStorageService = claimStorageService;
	}

}
